package com.yhh.location.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.List;

/*
 * @Description:
 * @Author: yhh
 * @Date: 2024/04/30 21:29
 * webSocket
 */
@Component
@ServerEndpoint("/websocket/location")
public class LocationWS {
    //List集合存放session地址
    private static List<Session> list = new ArrayList<>();

    //四种状态
    @OnOpen
    public void onOpen(Session session){
        System.out.println("连接成功");
        list.add(session);
    }
    @OnMessage
    public void onMessage(String message){}

    @OnClose
    public void onClose(Session session){
        //在list中移除session
        list.remove(session);
    }

    @OnError
    public void onError(Throwable error,Session session){
        list.remove(session);
    }
}
